package file

import (
	"compress/gzip"
	"io/ioutil"
	"os"
)

// GZipRead 读取内容
func GZipRead(source string) ([]byte, error) {
	file, err := os.Open(source)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	reader, err := gzip.NewReader(file)
	if err != nil {
		return nil, err
	}
	defer reader.Close()
	return ioutil.ReadAll(reader)
}

// GZipReadLine 按行读取内容
func GZipReadLine(source string, handle func([]byte) error) error {
	file, err := os.Open(source)
	if err != nil {
		return err
	}
	defer file.Close()
	reader, err := gzip.NewReader(file)
	if err != nil {
		return err
	}
	defer reader.Close()
	return ReaderLine(reader, handle)
}

// GZipWrite 写入内容
func  GZipWrite(source, name string, data []byte) error {
	file, err := os.Create(source)
	if err != nil {
		return err
	}
	writer := gzip.NewWriter(file)
	writer.Name = name
	defer writer.Close()
	_, err = writer.Write(data)
	return err
}
